"""
Case Type   : GUC
Case Name   : 修改enable_incremental_checkpoint为off，观察预期结果；
Create At   : 2020/11/23
Owner       : opentestcase018
Description :
    1、查询enable_incremental_checkpoint默认值;
    show enable_incremental_checkpoint;
    2、修改enable_incremental_checkpoint为off
    重启使其生效，建立mot表，观察预期结果。
    gs_guc set -N all -I all -c "enable_incremental_checkpoint=off"
    drop foreign table if exists test cascade;create foreign table test(x int);
    3、恢复默认值；
Expect      :
    1、查询默认值成功；
    2、修改参数成功，因mot表只支持增量检查点关闭状态下建表，
    此时建表成功，预期结果正常；
    3、恢复默认值成功；
History     :
    modified by opentestcase038,2023.02.13，修改设置参数的GUC命令避免备机参数值与
    主机参数值不同步的问题；
"""

import sys
import unittest
import os
from yat.test import macro
from yat.test import Node

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH

logger = Logger()
commonsh = CommonSH('PrimaryDbUser')


class Guctestcase(unittest.TestCase):
    def setUp(self):
        logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.userNode = Node('PrimaryDbUser')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.Constant = Constant()
        self.stopstartCmd = \
            f"source {macro.DB_ENV_PATH};gs_om -t stop && gs_om -t start"
        self.statusCmd = f"source {macro.DB_ENV_PATH};gs_om -t status --detail"
        self.table_name = "tab_wal_0028"

    def test_guc_wal(self):
        logger.info("-----step1:show enable_incremental_checkpoint;"
                    "expect:默认值on-----")
        sql_cmd = commonsh.execut_db_sql(
            f'''show enable_incremental_checkpoint;''')
        logger.info(sql_cmd)
        self.value = sql_cmd.splitlines()[-2].strip()

        logger.info("-----step2:方式一修改enable_incremental_checkpoint为off;"
                    "expect:设置成功-----")
        sql_cmd = \
            f"source {macro.DB_ENV_PATH};" \
            f"gs_guc set -N all -I all -c 'enable_incremental_checkpoint=off';"
        logger.info(sql_cmd)
        msg = self.userNode.sh(sql_cmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.GSGUC_SUCCESS_MSG, msg)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], msg)

        logger.info("-----show enable_incremental_checkpoint结果为off-----")
        msg = self.userNode.sh(self.stopstartCmd).result()
        logger.info(msg)
        self.assertIn(self.Constant.START_SUCCESS_MSG, msg)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], msg)
        status = self.userNode.sh(self.statusCmd).result()
        logger.info(status)
        sql_cmd = commonsh.execut_db_sql(
            f'''show enable_incremental_checkpoint;''')
        logger.info(sql_cmd)
        self.assertIn(self.Constant.CLOSE_STATUS_MSG[0], sql_cmd)

        logger.info("-----step3:create foreign table;expect:建表成功-----")
        sql_cmd = commonsh.execut_db_sql(
            f'''drop FOREIGN table if exists {self.table_name} cascade;
            create FOREIGN table {self.table_name}(x int);''')
        logger.info(sql_cmd)
        self.assertNotIn(self.Constant.SQL_WRONG_MSG[1], sql_cmd)
        self.assertIn(self.Constant.CREATE_FOREIGN_SUCCESS_MSG, sql_cmd)

    def tearDown(self):
        logger.info("-----step3:清理环境;expect:清理成功-----")
        logger.info("-----drop table-----")
        sql_cmd = commonsh.execut_db_sql(
            f'''drop FOREIGN table if exists {self.table_name} cascade;''')
        logger.info(sql_cmd)
        logger.info("-----reset enable_incremental_checkpoint-----")
        commonsh.execute_gsguc('set',
                               self.Constant.GSGUC_SUCCESS_MSG,
                               f'enable_incremental_checkpoint={self.value}')
        commonsh.restart_db_cluster()
        is_started = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in is_started or "Normal" in is_started)
        logger.info(f"-----{os.path.basename(__file__)} end-----")
